package com.asom.player.service;

import java.util.ArrayList;
import java.util.Random;

public class MediaPlaylist implements Comparable<MediaPlaylist>
{		
	ArrayList<Song>		songs;
	int					iCurrentSong;
	
	String 		id;
	String		name;
	
	boolean				shuffle;
	
	Song getCurrentSong()
	{		 
		return songs.get(iCurrentSong);
	}
	
	public MediaPlaylist(
			String _name)
	{		
		songs = new ArrayList<Song>();
		iCurrentSong = 0;
	
		name = _name;
	}
	
	public String getId()
	{
		return id;
	}
	
	public String getName()
	{
		return name;
	}
	
	public void play()
	{
		this.getCurrentSong().play();
	}
	
	public void play(Song _song)
	{
		iCurrentSong = songs.indexOf(_song);
		
		this.play();
	}
	
	public void pause()
	{
		this.getCurrentSong().pause();
	}
	
	public void stop()
	{
		this.getCurrentSong().stop();
	}
	
	public boolean isPlaying()
	{
		return this.getCurrentSong().isPlaying();
	}
	
	public void next()
	{
		if (shuffle)
		{
			Random random = new Random();
			
			iCurrentSong = random.nextInt(songs.size());
		}
		else
		{
			if (iCurrentSong < songs.size() - 1)
				iCurrentSong ++;
		}
		
		this.play();
	}
	
	public void prev()
	{
		if (iCurrentSong > 0)
			iCurrentSong --;
		
		this.play();
	}
	
	public void setShuffle(boolean _shuffle)
	{
		shuffle = _shuffle;
	}
	
	public boolean getShuffle()
	{
		return shuffle;
	}
	
	public boolean hasSong(Song _song)
	{
		return songs.contains(_song);
	}
	
	public void clear()
	{
		songs.clear();
	}
	
	public void addSong(Song _song)
	{
		songs.add(_song);
	}
	
	public void removeSong(Song _song)
	{
		songs.remove(_song);
	}
	
	public Song[] getSongs()
	{
		return songs.toArray(new Song[songs.size()]);
	}

	@Override
	public int compareTo(MediaPlaylist another)
	{
		return this.getName().compareTo(another.getName());
	}
}
